<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CashController extends Controller
{

    //代理佣金提现列表
    public function cashproxylist(Request $request)
    {
        $id=session('user_id');
        $search=$request->cashname;
        if ($search) {//填写搜索条件时
            $cashproxylist=\DB::table('cash')->where(array('is_success'=>'1','type'=>'1'))->where(function($sql1) use($search){
                $sql1->orwhere('user_name',$search)->orwhere('user_phone',$search);
            })->paginate(15);
            $cashproxycount=\DB::table('cash')->where(array('is_success'=>'1','type'=>'1'))->where(function($sql1) use($search){
                $sql1->orwhere('user_name',$search)->orwhere('user_phone',$search);
            })->count();
        }else{//未填写时
            $cashproxylist=\DB::table('cash')->where(array('is_success'=>'1','type'=>'1'))->paginate(15);
            $cashproxycount=\DB::table('cash')->where(array('is_success'=>'1','type'=>'1'))->count();
        }
        return view('admin.cash.cashproxylist',compact('cashproxylist','cashproxycount'));
    }
    //同意已处理
    public function agreecash(Request $request)
    {
        $id=session('user_id');
        $theid=$request->cashid;
        $res=\DB::table('cash')->where(array('id'=>$theid))->update(['is_success'=>'2','examine_time'=>date('Y-m-d H:i:s',time())]);
        if ($res) {
            $array = array('msg'=>'处理成功!','status'=>'true');
            return json_encode($array);
        }else{
            $array = array('msg'=>'处理失败!','status'=>'false');
            return json_encode($array);
        }
    }
    //拒绝提现
    public function disagreecash(Request $request){
        $id=session('user_id');
        $theid=$request->cashid;
        //把资金返还给用户
        $getuserid=\DB::table('cash')->where(array('id'=>$theid))->value('user_id');
        $getbrokerage=\DB::table('cash')->where(array('id'=>$theid))->value('money');
        $gobackto=\DB::table('user')->where(array('user_id'=>$getuserid))->increment('brokerage',$getbrokerage);
        $res=\DB::table('cash')->where(array('id'=>$theid))->update(['is_success'=>'2','examine_time'=>date('Y-m-d H:i:s',time())]);
        if ($gobackto&&$res) {
            $array = array('msg'=>'处理成功!','status'=>'true');
            return json_encode($array);
        }else{
            $array = array('msg'=>'处理失败!','status'=>'false');
            return json_encode($array);
        }
    }
    //平台佣金提现列表
    public function cashlist(Request $request){
        $id=session('user_id');
        $search=$request->cashname;
        if ($search) {//填写搜索条件时
            $cashlist=\DB::table('cash')->where(array('is_success'=>'1','type'=>'2'))->where(function($sql1) use($search){
                $sql1->orwhere('user_name',$search)->orwhere('user_phone',$search);
            })->paginate(15);
            $cashcount=\DB::table('cash')->where(array('is_success'=>'1','type'=>'2'))->where(function($sql1) use($search){
                $sql1->orwhere('user_name',$search)->orwhere('user_phone',$search);
            })->count();
        }else{//未填写时
            $cashlist=\DB::table('cash')->where(array('is_success'=>'1','type'=>'2'))->paginate(15);
            $cashcount=\DB::table('cash')->where(array('is_success'=>'1','type'=>'2'))->count();
        }
        return view('admin.cash.cashlist',compact('cashlist','cashcount'));
    }
    //同意-已转账
    public function agreetoget(Request $request){
        $theid=$request->cashid;
        $res=\DB::table('cash')->where(array('id'=>$theid))->update(['is_success'=>'2','examine_time'=>date('Y-m-d H:i:s',time())]);
        if ($res) {
            $array = array('msg'=>'处理成功!','status'=>'true');
            return json_encode($array);
        }else{
            $array = array('msg'=>'处理失败!','status'=>'false');
            return json_encode($array);
        }
    }
    //拒绝
    public function disagreetoget(Request $request){
        $theid=$request->cashid;
        //把资金返还给用户
        $getuserid=\DB::table('cash')->where(array('id'=>$theid))->value('user_id');
        $getbrokerage=\DB::table('cash')->where(array('id'=>$theid))->value('money');
        $gobackto=\DB::table('user')->where(array('user_id'=>$getuserid))->increment('advance_money',$getbrokerage);
        $res=\DB::table('cash')->where(array('id'=>$theid))->update(['is_success'=>'2','examine_time'=>date('Y-m-d H:i:s',time())]);
        if ($gobackto&&$res) {
            $array = array('msg'=>'处理成功!','status'=>'true');
            return json_encode($array);
        }else{
            $array = array('msg'=>'处理失败!','status'=>'false');
            return json_encode($array);
        }
    }
}